package prj.carmap.mobile.process;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import prj.carmap.mobile.CarmapApplication;
import prj.carmap.mobile.activity.SellerListActivity;
import prj.carmap.mobile.activity.SellerSearchActivity;
import prj.carmap.mobile.model.SellerInfo;
import android.content.Intent;

import com.loopj.android.http.AsyncHttpResponseHandler;

/**
 * 商家搜索列表处理
 *
 */
public class SellerSearchProcess extends AsyncHttpResponseHandler {
	private SellerSearchActivity context;

	public SellerSearchProcess(SellerSearchActivity context) {
		this.context = context;
	}

	public void onSuccess(String content) {
		try
		{
			JSONArray jsonAry = new JSONArray(content);
			ArrayList<SellerInfo> sellers = new ArrayList<SellerInfo>();
			for (int i = 0; i < jsonAry.length(); i++) {
				JSONObject json = jsonAry.getJSONObject(i);
				sellers.add(SellerInfo.fromJson(json));
			}
			Intent intent = new Intent(context, SellerListActivity.class);
			intent.putExtra("sellers", sellers);
			intent.putExtra("title", "关键字:" + context.searchCond);
			intent.putExtra("sellerName", context.searchCond);
			intent.putExtra("url", CarmapApplication.SERVER_URL + "/rest/seller/search/"
					+ context.searchCond
					+ "/"
					+ CarmapApplication.getInstance()
							.getLocData().longitude
					+ "/"
					+ CarmapApplication.getInstance()
							.getLocData().latitude);
			context.startActivity(intent);
		}
		catch (JSONException e) {
			e.printStackTrace();
		}
		CarmapApplication.popView.hide();
	}

	public void onFailure(Throwable error, String content) {
		CarmapApplication.popView.hide();
	}

}
